#include "StdAfx.h"
#include "GroundRand.h"
#include "BattleGround.h"
GroundRand::GroundRand(BattleGround& Bg,int nRandSeed):
m_rBg(Bg),
m_nRandSeed(nRandSeed)
{
}

GroundRand::~GroundRand(void)
{
}

int GroundRand::Rand( int nEntityID )
{
	int nRand(0);
	if(nEntityID == 0)
	{
		nRand = rand();
	}
	else
	{
		std::map<int,CMHRand>::iterator itMHRand = m_mapEntityRand.find(nEntityID);
		if(itMHRand!= m_mapEntityRand.end())
		{
			nRand = itMHRand->second.Rand();
		}
		else
		{
			nRand = m_mapEntityRand[nEntityID].Rand();
		}
	}
	return nRand;
}

unsigned int GroundRand::GetRandomSeed( int nEntityID )
{
	unsigned int nSeed(0);
	std::map<int,CMHRand>::iterator itMHRand = m_mapEntityRand.find(nEntityID);
	if(itMHRand!= m_mapEntityRand.end())
	{
		nSeed = itMHRand->second.GetRandomSeed();
	}
	return nSeed;
}

void GroundRand::SynRsp( int nEntityID,unsigned int nSeed ,unsigned int nTimes)
{
	if( nTimes == m_nSaveTimes ) 
	{
		if(nReqFep < 100)
			nReqFep++;

	}
	else
	{
		std::map<int,CMHRand>::iterator itMHRand = m_mapEntityRand.find(nEntityID);
		if(itMHRand!= m_mapEntityRand.end())
		{
			CMHRand& rMHRand = itMHRand->second;
			int nLeftTimes = rMHRand.GetRandomTimes() - m_nSaveTimes;
			rMHRand.srand(nSeed,nTimes);
			while( nLeftTimes >= 0)
			{
				rMHRand.Rand();
			}
		}

		if(nReqFep > 10)
			nReqFep--;	
	}

	
}

void GroundRand::SaveCurrent( int nEntityID )
{
	unsigned int nSeed(0);
	std::map<int,CMHRand>::iterator itMHRand = m_mapEntityRand.find(nEntityID);
	if(itMHRand!= m_mapEntityRand.end())
	{
		m_nSaveTimes = itMHRand->second.GetRandomTimes();
	}
}

unsigned int GroundRand::GetRandomTimes( int nEntityID )
{
	unsigned int nTimes(0);
	std::map<int,CMHRand>::iterator itMHRand = m_mapEntityRand.find(nEntityID);
	if(itMHRand!= m_mapEntityRand.end())
	{
		nTimes = itMHRand->second.GetRandomTimes();
	}
	return nTimes;
}